import { Keyframes } from 'ant-design-vue';
import axios from 'axios';


const columns= [
  {
    title: 'pluginId',
    dataIndex: 'pluginId',
    key: 'pluginId',
  },
  {
    title: 'cron',
    dataIndex: 'cron',
    key: 'cron',
  },
  {
    title: 'route',
    dataIndex: 'route',
    key: 'route',
  },
  {
    title: 'lastRunAt',
    dataIndex: 'lastRunAt',
    key: 'lastRunAt',
  },
  {
    title: 'lastRunResult',
    dataIndex: 'lastRunResult',
    key: 'lastRunResult',
  },
  {
    title: 'operation',
    dataIndex: 'operation',
    key: 'operation',
  }
]

const configColumns = [
  {
    title: 'key',
    dataIndex: 'key',
    key: 'key',
  },
  {
    title: 'value',
    dataIndex: 'value',
    key: 'value',
  },
  {
    title: 'operation',
    dataIndex: 'operation',
    key: 'operation',
  }
]

function bodyBuild(pluginId, k, v) {
  return {
    pluginId, k, v
  }
}

class Key {
  constructor () {
    this.show = false
    this.list = []
    this.keyToNew = {
      key: '',
      value: ''
    },
    this.targetPlugin = ''
    this.targetRoute = ''
  }

  freshKeyList () {
    axios.get(`/plugin/list/key?pluginId=${this.targetPlugin}`).then(res => {
      if (res.data.code === 200) {
        this.list = res.data.data
      }
    }).catch(err => err)
  }

  openKeyPanel (plugin) {
    this.targetPlugin = plugin.pluginId
    this.targetRoute = plugin.route
    console.log(this)
    this.list = plugin.keyList
    this.freshKeyList()
    this.show = true
  }

  newKey () {
    var body = bodyBuild(this.targetPlugin, this.keyToNew.key, this.keyToNew.value)
    axios.post(`/plugin/add/key`, body).then(res => {
      if (res.data.code === 200) {
        this.freshKeyList()
        this.keyToNew = {
          key: '',
          value: ''
        }
      }
    }).catch(err => err)
  }

  editKey (k) {
    var res = prompt('new value')
    if (res != null) {
      this.keyToNew = {
        key: k,
        value: res
      }
      this.newKey()
    }
  }

  delKey (key) {
    var body = bodyBuild(this.targetPlugin, key, null)
    axios.post(`/plugin/del/key`,  body).then(res => {
      if (res.data.code === 200) {
        this.freshKeyList()
      }
    }).catch(err => err)
  }
}

class Config {
  constructor () {
    this.show = false
    this.list = []
    this.configToNew = {
      key: '',
      value: ''
    },
    this.targetPlugin = ''
  }

  freshConfigList () {
    axios.get(`/plugin/list/config?pluginId=${this.targetPlugin}`).then(res => {
      if (res.data.code === 200) {
        this.list = res.data.data
      }
    }).catch(err => err)
  }

  openConfigPanel (plugin) {
    this.targetPlugin = plugin.pluginId
    this.list = plugin.configList
    this.freshConfigList()
    this.show = true
  }

  newConfig () {
    var body = bodyBuild(this.targetPlugin, this.configToNew.key, this.configToNew.value)
    axios.post(`/plugin/add/config`, body).then(res => {
      if (res.data.code === 200) {
        this.freshConfigList()
        this.configToNew = {
          key: '',
          value: ''
        }
      }
    }).catch(err => err)
  }

  editConfig (k) {
    var res = prompt('new value')
    if (res != null) {
      this.configToNew = {
        key: k,
        value: res
      }
      this.newConfig()
    }
  }

  delConfig (k) {
    var body = bodyBuild(this.targetPlugin, k, null)
    axios.post(`/plugin/del/config`, body).then(res => {
      if (res.data.code === 200) {
        this.freshConfigList()
      }
    }).catch(err => err)
  }
}

export {
  Key, Config, columns, configColumns
}